package com.future.lock.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    public static String APP_ID = Constants.APP_KEY;
    private IWXAPI api;
    /**
     * 设置当前请求付款的返回响应监听器
     */
    private static IWXAPIEventHandler sIwxapiEventHandler = null;

    /**
     * 设置当前的监听器
     *
     * @param handler
     */
    public static void setWXAPIEventHandler(IWXAPIEventHandler handler) {
        sIwxapiEventHandler = handler;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, WXPayEntryActivity.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Log.d("WXPayEntryActivity", "onReq");
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d("WXPayEntryActivity", "onResp");
        //        OrderPayPage.sWxResp = resp;
        finish();
        notifyPayResultEvent(resp);
    }

    /**
     * 通知之前设定的监听器微信处理结果
     *
     * @param resp
     */
    private void notifyPayResultEvent(BaseResp resp) {
        if (sIwxapiEventHandler != null) {
            sIwxapiEventHandler.onResp(resp);
            sIwxapiEventHandler = null;
        }
    }
}